$(function(){ $('body').toggleClass('msie', !!$.browser.msie); App.Publish('app/ready'); });

var App=App||{};

App.Publish = function(channel, args){ $.publish(channel, args); };
App.Subscribe = function(channel, callback, scope){ $.subscribe(channel, callback, scope || App); };


/**
 * Ajax short-cut
 * @param url			url to request => assist/{url}
 * @param data			data will be JSON.stringified
 * @param scope			scope for callback - "App" by default
 * @param callback		callback function
 */
App.Request = function(url, data, callback, scope){
	$.ajax({ url:'assist/'+url, data: JSON.stringify(data), type: 'post', dataType: 'json', mimeType:'application/json', contentType: 'application/json', error: App.Error })
	.success(function(data){
		if (typeof data !== 'object') return App.Error('Server response error');												// response can be {}, [] or null
		if (data && data.result == 'error') return App.Error(data);
		if (data && data.result == 'warning') App.Error(data);
		if (callback && typeof callback === 'function') {
			if (scope && scope.callback && typeof scope.callback === 'function') scope.callback.call(scope, data);				// execute in a given scope 
			else callback.call(callback, data); 																				// execute in self scope
		}
	});
};


/**
 * Confirmation dialog shortcut
 * @param conf			configuration object, see "defaults" for options
 */
App.Confirm = function(conf){
	var defaults = { title: 'Confirm', text: 'Are you sure?', 
		submitLabel: 'Confirm', submitFn : function(){},
		cancelLabel: 'Cancel', cancelFn : function(){}
	};
	conf = $.extend(defaults, conf);
	if (!conf.scope || typeof conf.scope !== 'function') conf.scope =  conf.submitFn;
		
	new XWindow({ title: conf.title, disposable: true, content: conf.text, buttons: [
		{ text: conf.submitLabel, scope: conf.scope, fn: conf.submitFn, cls: 'submit' },
		{ text: conf.cancelLabel, scope: conf.scope, fn: conf.cancelFn, cls: 'btn-cancel' }
	]}).show();
};


/**
 * Confirmation dialog shortcut
 * @param conf			configuration object, see "defaults" for options
 */
App.Alert = function(conf){
	conf = $.extend({ title: 'Warning', text: '', btnLabel: 'OK' }, conf);	
	new XWindow({ title: conf.title, disposable: true, content: conf.text, buttons: [{ text: conf.btnLabel, cls: 'submit' } ]}).show();
	return false;
};

/**
 * Toaster message short-cut
 * @param msg		text message to show
 * @param type		type of the message [success, info | error | warning,alert]; (defaults to: success)
 */
App.Msg = function(conf, type){ Toaster(conf, type); };

/**
 * Global error handler
 * @param msg	msg object: { result: 'error', type:'', details: 'message details' }
 */
App.Error = function(msg){
	if (!msg) App.Msg('Unknown server error.', 'error');
	else if (typeof msg === 'string') App.Msg({type:'error', text: msg});
	else {
		if (msg.status == 404 || msg.status == 400) App.Msg('Requested service not available', 'error');
		else if (msg.type == 'authentication') {
			App.Msg({ type: 'error', text: msg.details + ' <a href="#" onclick="location.reload()">Please log-in again!</a>', autoClose: false });
			$('body').mask(null,0,10000);
		}
		else {
			if (msg.details) App.Msg({ type: (msg.result=='warning'?'warning':'error'), text: msg.details, delay: 5 });
			else App.Msg('Unknown server error.', 'error');
		}
	}
	return false;
};

App.toString = function(){ return 'PROSPER ASSIST - Main Module'; };
